home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12591 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news.luc.edu!user
  2. From: VArase@varase.it.luc.edu (Verne Arase)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Why does my program do this?
  5. Date: Mon, 01 Apr 1996 16:16:50 -0600
  6. Organization: LUMC
  7. Message-ID: <AD85B1F29668E2CE7@mcdiala11.it.luc.edu>
  8. References: <4jnln2$95j@dfw-ixnews3.ix.netcom.com>
  9. NNTP-Posting-Host: 147.126.240.111
  10.  
  11. In article <4jnln2$95j@dfw-ixnews3.ix.netcom.com>,
  12. ishky@ix.netcom.com(Andrew Heiz ) wrote:
  13.  
  14.  >    for (i=0; i <= students-1; i=i+1)
  15.  
  16. Might I suggest:
  17.  
  18.    for (i=0; i<students; i++)
  19.  
  20.  >char scores[STUDENT][5];
  21.  > ...
  22.  >    for (i=0; i <= students-1; i=i+1)
  23.  >        {
  24.  >            for (j=0; j <= tests-1; j=j+1)
  25.  >            {
  26.  >                gets(&scores[i][j];
  27.  >            }
  28.  >        }
  29.  
  30. I believe what you really want is to storage your score values in an
  31. arithmentic type (like short). Alternately, you'll want a three dimensional
  32. character matrix, as there is no intrinsic string type in C and you must
  33. use a character vector instead. Therefore, scores (in character form) would
  34. look something like the following:
  35.  
  36.    char scores[MAXSTUDENTS][MAXGRADES][MAXLENGTH+1]
  37.  
  38.  >    for (i=0; i <= students-1; i=i+1)
  39.  >        {
  40.  >        printf("Scores for student %d",i+1);
  41.  >            for (j=0; j <= tests-1; j=j+1)
  42.  >            {
  43.  >                printf("\tGrade %d: %s",j+1,scores[i][j]);
  44.  >            }
  45.  >        }
  46.  
  47. The %s is directing printf to print a string. In C, a string is a character
  48. vector (or pointer) with a run of characters terminated by a NUL ('\0').
  49.  
  50. ---
  51. The above are my own opinions, and not those of my employer.
  52.